package com.spider.silence.main;

import com.alibaba.fastjson.JSONObject;
import com.spider.silence.downloader.ChinaNewsForSiChuanDownloader;
import com.spider.silence.pool.ChinaNewsForSiChuanDownloaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.ChinaNewsForSiChuanWorker;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Linbo Hu on 2017/6/27.
 * 中国新闻网四川频道主方法
 */
public class ChinaNewsForSiChuanMain {

    public static void main(String[] args) throws InterruptedException, IOException {
        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        ChinaNewsForSiChuanDownloaderPool chinaNewsForSiChuanDownloaderPool = ChinaNewsForSiChuanDownloaderPool.getInstance();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取一个下载器
                ChinaNewsForSiChuanDownloader downloader = null;
                try {
                    downloader = chinaNewsForSiChuanDownloaderPool.fetchDownLoader(1000);
                    downloader.generateTask();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if(downloader != null) {
                        chinaNewsForSiChuanDownloaderPool.releaseDownLoader(downloader);
                    }
                }
            }
        }).start();

        Thread.sleep(5000);
        //new Thread(new ChinaNewsForSiChuanWorker()).start();
        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //初始化中国新闻网四川频道的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("ChinaNewsForSiChuan.workCount")); i++){
            pool.execute(new ChinaNewsForSiChuanWorker());
        }


   /* ChinaNewsForSiChuanDownloader downloader = new ChinaNewsForSiChuanDownloader();
        while (true) {
            try {
                JSONObject object = downloader.crawlItem("http://www.sc.chinanews.com/scxw/2017-05-27/69642.html");
                System.out.println(object.toJSONString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
    }

}
